package vp.remedies.dr.home.activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.adapter.LazyAdapter;
import vp.remedies.dr.home.listviewfeed.data.FeedItem;
import vp.remedies.dr.home.utils.XMLParser;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainRemediesActivity extends Activity {

	public static final String KEY_REMEDY = "remedy"; // parent node
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_ACTIVITY_NAME = "activity_name";
	public static final String KEY_XML_FILE_NAME = "xml_file_name";
	
	ListView list;
    LazyAdapter adapter;
    
    public String getAssetXMLFile(String xmlFileName) throws IOException {
		AssetManager manager = this.getAssets();
		InputStream file = manager.open(xmlFileName);
		byte[] xmlContentBytes = new byte[file.available()];
		file.read(xmlContentBytes);
		file.close();

		String xmlString = new String(xmlContentBytes);
		return xmlString;
	}
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_remedy_list);
		
		String xmlFileName = (String) getIntent().getExtras().get(KEY_XML_FILE_NAME);
		final ArrayList<HashMap<String, String>> mainRemedyList = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		
		String data = null;
		try {
			data = getAssetXMLFile(xmlFileName);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Document doc = parser.getDomElement(data); // getting DOM element
		
		NodeList nl = doc.getElementsByTagName(KEY_REMEDY);
		// looping through all song nodes <song>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
			map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
			map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
			map.put(KEY_ACTIVITY_NAME, parser.getValue(e, KEY_ACTIVITY_NAME));

			// adding HashList to ArrayList
			mainRemedyList.add(map);
		}
		

		list=(ListView)findViewById(R.id.list);
		list.setSelector(R.drawable.list_selector_new);
		// Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, mainRemedyList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// On selecting single track get song information

				HashMap<String, String> remedyMap = mainRemedyList.get(position);
				// Toast.makeText(activity, htmlPageName, Toast.LENGTH_LONG).show();
				String activityName = remedyMap.get(KEY_ACTIVITY_NAME);
				Intent i;
				try {
					i = new Intent(view.getContext(), Class.forName(activityName));
					startActivity(i);		
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});		
	}	
}